import javax.swing.JFrame;

// John Dennis

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

//import AgendaView.ButtonActionListener;

// Questions to ask Ben: howw to build exact same screen for Agenda View

public class AgendaView {

	private PhoneLayout phone; // incorporates the standard phone interface

	private JPanel cards;
	
	private JPanel agendaView;
	private JPanel fullScreenCards;
	private JPanel appsPanel;
	private JPanel seperator;

	private static ButtonActionListener buttonListener;

	private List<Appointment> apps;
	private Appointment current_app;
	private JLabel appLabel;
	

	private CalendarDate currentDate, endDate;

	public AgendaView(PhoneLayout phone) {
		this.phone = phone;
		cards = phone.middleCards;

		buttonListener = new ButtonActionListener();
		
		agendaView = new JPanel();

		//agendaView.setBorder(new EmptyBorder(20, 10, 20, 10)); // padding
		agendaView.setBackground(Color.white);
		agendaView.setPreferredSize(new Dimension(480, 640));

		CalendarDate start = new CalendarDate(12, 03, 1992);
		endDate = new CalendarDate(31, 12, 2011);

		appsPanel = new JPanel();
		appsPanel.setPreferredSize(new Dimension(480, 400));
		appsPanel.setLayout(new GridLayout(10, 1));
		appsPanel.setBackground(Color.white);
		
		apps = phone.cal.getAppointmentsBetweenDates(start, endDate);
		Collections.sort(apps);
		appsPanel = createAppPanel(apps);

		currentDate = phone.date;

		JScrollPane scrollpane = new JScrollPane(appsPanel);
		
		agendaView.add(scrollpane);
		cards.add(agendaView, "agenda");
		appsPanel.revalidate();
		appsPanel.repaint();
		
		
		
	}

	/*
	 * creates label for a new date
	 */
	public static JLabel createDateLabel(CalendarDate currentDate) {
		JLabel label = new JLabel("    " +currentDate.toString());
		label.setFont(new Font("Arial", Font.PLAIN, 12));
		label.setPreferredSize(new Dimension (480,20));
		label.setBackground(Color.pink);
		label.setOpaque(true);
		return (label);
	}

	/*
	 * creates a seperator line each time we have a new appointment
	 */
	public static JPanel createSeperator(int thickness) {
		JPanel seperator = new JPanel();
		seperator.setPreferredSize(new Dimension(480, thickness));
		seperator.setBackground(Color.darkGray);
		return (seperator);
	}

	/*
	 * creates panels for each appointment implements these panels with button
	 * functionality
	 */
	public static JPanel createAppPanel(List<Appointment> apps) {
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(480, 600));
		panel.setBackground(Color.white);
		CalendarDate currentDate = apps.get(0).date;
		panel.add(createDateLabel(currentDate));
		for (Appointment app : apps) {
			if (!currentDate.equals(app.date)) {
				currentDate = app.date;
				panel.add(createDateLabel(currentDate));
			}
			panel.add(createAppPanel(app)); // also includes button
											// functionality
			panel.add(createSeperator(1));
		}

		return (panel);
	}

	/*
	 * creates panel for appointment with button functionality called in the
	 * createAppPanel method with list argument
	 */
	public static JPanel createAppPanel(Appointment app) {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setPreferredSize(new Dimension(480, 40));
		panel.setBackground(Color.white);
		JLabel description = new JLabel("     " + app.description);
		JLabel location = new JLabel("   " + app.location +"  ");
		location.setFont(new Font("Arial", Font.ITALIC,11));
		JLabel time = new JLabel("  "+timeStr(app.start_time) + " - "
				+ timeStr(app.end_time));
		time.setFont(new Font("Arial", Font.BOLD, 16));
		JButton button = new JButton();
		button.setText(app.getID()+ "");
		button.setSize(new Dimension(480, 50));
		button.setOpaque(false);
		button.setContentAreaFilled(false);
		button.setBorderPainted(false);
		button.setFont(new Font("arial", Font.ITALIC, 0));
		button.addActionListener(buttonListener);
		panel.add(button);
		panel.add(BorderLayout.WEST, time);
		panel.add(BorderLayout.CENTER, description);
		panel.add(BorderLayout.EAST, location);
		return (panel);
	}

	public static String timeStr(CalendarTime time) {
		String timeStr[] = time.toString().split(":");

		if (timeStr[0].length() < 2) {
			timeStr[0] = "0" + timeStr[0];
		}

		if (timeStr[1].length() < 2) {
			timeStr[1] += "0";
		}

		return (timeStr[0] + ":" + timeStr[1]);
	}
	
	public void refreshAgenda(){
		AgendaView newone = new AgendaView(phone);
		appsPanel.revalidate();
		appsPanel.repaint();
		agendaView.revalidate();
		agendaView.repaint();
	
		
		
		
	}

	class ButtonActionListener implements ActionListener {

		public ButtonActionListener() {

		}

		public void actionPerformed(ActionEvent ae){ 
			CardLayout cl = (CardLayout)(phone.cards.getLayout());
			JButton button = (JButton) ae.getSource();
			phone.editScreen.editEventID(Integer.parseInt(button.getText())); // the text is the id
			cl.show(phone.cards, "EDIT_EVENT");

		}
	}
}